//
//  NovelViewModel.m
//  novel
//
//  Created by Tracy on 2018/8/11.
//  Copyright © 2018年 Tracy. All rights reserved.
//

#import "NovelViewModel.h"

@implementation NovelViewModel

-(NSMutableArray *)loadData {
    
    NSData *JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ge" ofType:@"json"]];
    
    NSDictionary *dataDic= [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];
    NSArray * allData= [dataDic objectForKey:@"body"];
#pragma mark -- 轮播数据
    NSDictionary *lunboDic = allData[0];
    NSArray * lunboArray = [lunboDic objectForKey:@"items"];
    
    for (NSDictionary * dic in lunboArray) {
        novelHeaderModel * headerModel = [novelHeaderModel modelWithJSON:dic];
        [self.headerSource addObject:headerModel];
    }
#pragma mark -- 第一层数据
    NSDictionary *firstDic = allData[1];
    NSArray * firstArray = [firstDic objectForKey:@"items"];
    for (NSDictionary *dic in firstArray) {
        novelFirstModel * model = [novelFirstModel modelWithJSON:dic];
        [self.fistFloorDataSource addObject:model];
    }
  
#pragma mark -- 第二层数据
    NSDictionary *secondDic = allData[2];
    NSArray * secondArray = [secondDic objectForKey:@"items"];
    for (NSDictionary *dic in secondArray) {
        novelSecondModel * model = [novelSecondModel modelWithJSON:dic];
        [self.secondFloorDataSource addObject:model];
    }
    
#pragma mark -- 第三次数据
    for (NSInteger i = 3; i <allData.count; i++) {
        NSDictionary * allThirdDic = allData[i];
        NSMutableArray *thirdData = [NSMutableArray array];
        NSDictionary * sectionDic = [allThirdDic objectForKey:@"section"];
        NSString * sectionTitle = [sectionDic objectForKey:@"name"];
        NSArray *allThirdArray = [sectionDic objectForKey:@"books"];
        for (NSDictionary * dic in allThirdArray) {
            novelThirdModel * model = [novelThirdModel modelWithJSON:dic];
            [thirdData addObject:model];
            
        }
        NSDictionary * thirdDic = @{
                                       @"section":sectionTitle,
                                       @"dataSource":thirdData
                                       };
        [self.thiredFloorDataSource addObject:thirdDic];
    }
    
    self.novelDataSource = @[self.headerSource,self.fistFloorDataSource,self.secondFloorDataSource,self.thiredFloorDataSource].mutableCopy;
    
    return self.novelDataSource;
}

#pragma mark -- lazy load
- (NSMutableArray *)novelDataSource {
    if(!_novelDataSource){
        _novelDataSource = [NSMutableArray array];
    }
    return _novelDataSource;
}

- (NSMutableArray *)headerSource {
    if(!_headerSource){
        _headerSource = [NSMutableArray array];
    }
    return _headerSource;
}

- (NSMutableArray *)fistFloorDataSource {
    if(!_fistFloorDataSource){
        _fistFloorDataSource = [NSMutableArray array];
    }
    return _fistFloorDataSource;
}

-(NSMutableArray *)secondFloorDataSource {
    if(!_secondFloorDataSource){
        _secondFloorDataSource = [NSMutableArray array];
    }
    return _secondFloorDataSource;
}

- (NSMutableArray *)thiredFloorDataSource {
    if(!_thiredFloorDataSource){
        _thiredFloorDataSource = [NSMutableArray array];
    }
    return _thiredFloorDataSource;
}

@end
